package com.java2020.online_mall.controller;

import com.google.gson.Gson;
import com.java2020.online_mall.model.*;
import com.java2020.online_mall.service.GoodsService;
import com.java2020.online_mall.service.GoodsServiceImpl;
import com.java2020.online_mall.utils.FileUploadUtils;
import com.java2020.online_mall.utils.HttpUtils;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @PackgeName: ${PACKAGE_NAME}
 * @Author: MNHK
 * Date: 2020/5/2 21:40
 * @Version:1.8
 */

@WebServlet("/api/admin/goods/*")
public class GoodsServlet extends HttpServlet {

    private GoodsService goodsService = new GoodsServiceImpl();
    Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String correct = requestURI.replace("/api/admin/goods/", "");
        if ("imgUpload".equals(correct)){
            imgUpload(request,response);
        }else if ("addGoods".equals(correct)) {
            addGoods(request,response);
        }else if ("addSpec".equals(correct)) {
            addSpec(request,response);
        }else if ("addType".equals(correct)) {
            addType(request,response);
        }else if ("deleteSpec".equals(correct)) {
            deleteSpec(request,response);
        }else if ("updateGoods".equals(correct)) {
            updateGoods(request,response);
        }
    }

    private void updateGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        GoodsParam goodsParam = gson.fromJson(requestBody, GoodsParam.class);
        int code = goodsService.updateGoods(goodsParam);
        Result result = new Result();
        if(code == 200){
            result.setCode(0);
        }else {
            result.setMessage("服务器异常，更新失败");
            result.setCode(10000);
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void deleteSpec(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        GoodsSpec goodsSpec = gson.fromJson(requestBody, GoodsSpec.class);
        int code = goodsService.deleteSpec(goodsSpec);
        Result result = new Result();
        if(code == 200){
            result.setCode(0);
        }else {
            result.setCode(10000);
            result.setMessage("服务器异常，删除失败");
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void addType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        GoodsType goodsType = gson.fromJson(requestBody, GoodsType.class);
        int code = goodsService.addType(goodsType);

        Result result = new Result();
        if(code == 200){
            result.setCode(0);
        }else if(code == 500){
            result.setCode(10000);
            result.setMessage("商品类型重复，请重新输入");
        }else{
            result.setMessage("服务器异常");
            result.setCode(10000);
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void addSpec(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        GoodsSpec goodsSpec = gson.fromJson(requestBody, GoodsSpec.class);
        int code =goodsService.addSpec(goodsSpec);
        Result result = new Result();

        if (code == 200) {
            result.setCode(0);
            result.setData(goodsSpec);
        }else if (code == 500) {
            result.setCode(10000);
            result.setMessage("商品规格信息重复，请重新输入");
        }else {
            result.setCode(10000);
            result.setMessage("服务器异常");
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void addGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        GoodsParam goodsParam = gson.fromJson(requestBody, GoodsParam.class);
        goodsService.addGoods(goodsParam);
        Result result = new Result();
        result.setCode(0);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 商品图片上传
     * @param request
     * @param response
     * @throws IOException
     */
    private void imgUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Result result = new Result();
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (!isMultipart) {
            result.setCode(10000);
            result.setMessage("请上传图片类型的文件！");
            response.getWriter().println(gson.toJson(result));
            return;
        }
        Map<String, Object> map = FileUploadUtils.parseRequest(request);

        String fileName = (String) map.get("file");
        String domain = (String) getServletContext().getAttribute("domain");
        //文件路径 = 原文件名 + 域名/端口号
        fileName = domain + fileName;
        result.setCode(0);
        result.setData(fileName);
        response.getWriter().println(gson.toJson(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String correct = requestURI.replace("/api/admin/goods/", "");

        if("getType".equals(correct)){
            getType(request, response);
        }else if("getGoodsByType".equals(correct)){
            getGoodsByType(request, response);
        }else if("deleteGoods".equals(correct)){
            deleteGoods(request, response);
        }else if("deleteType".equals(correct)){
            deleteType(request, response);
        }else if("getGoodsInfo".equals(correct)){
            getGoodsInfo(request, response);
        }
    }

    private void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        Result result = new Result();
        if (!HttpUtils.isNumber(id)) {
            result.setCode(0);
            result.setMessage("ID不合法！");
            response.getWriter().println(gson.toJson(result));
            return;
        }
        Map<String, Object> resultMap = goodsService.getGoodsInfo(id);
        result.setCode(0);
        result.setData(resultMap);
        response.getWriter().println(gson.toJson(result));
    }

    private void deleteType(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("typeId");
        Result result = new Result();

        if(! HttpUtils.isNumber(id)){
            result.setCode(10000);
            result.setMessage("请求数据格式错误");
        }else {
            int code = goodsService.deleteType(id);
            if (code == 200) {
                result.setCode(0);
            } else if (code == 501) {
                result.setCode(10000);
                result.setMessage("改类目下有商品，不允许删除");
            } else {
                result.setCode(10000);
                result.setMessage("服务器异常，删除失败");
            }
        }
    }

    private void deleteGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String goodsId = request.getParameter("id");
        Result result = new Result();
        if ( !HttpUtils.isNumber(goodsId)) {
            result.setCode(10000);
            result.setMessage("参数格式错误！");
            response.getWriter().println(gson.toJson(result));
            return;
        } else {
            int code = goodsService.deleteGoods(goodsId);
            if (code == 200) {
                result.setCode(0);
            } else {
                result.setMessage("服务器异常，删除失败");
                result.setCode(10000);
            }
        }
        response.getWriter().println(gson.toJson(result));
    }


    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");
        List<Goods> goodsList = goodsService.getGoodsByType(typeId);
        Result result = new Result();
        result.setCode(0);
        result.setData(goodsList);
        response.getWriter().println(gson.toJson(result));
    }

    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<GoodsType> typeList = goodsService.getType();
        Result result = new Result();
        result.setCode(0);
        result.setData(typeList);
        response.getWriter().println(gson.toJson(result));
    }
}
